<?php
//$loader->requireOnce("/lib/Calendar/Month.php");
$loader->requireOnce("/lib/Calendar/Month/Weekdays.php");
$loader->requireOnce("/lib/Calendar/Week.php");
$loader->requireOnce("/lib/Calendar/Day.php");
class WorkingCalendar {
	private $year = '';
	private $month = '';
	private $day = '';
		
	function WorkingCalendar($year, $month, $day = '') {
		$this->year = $year;
		$this->month = $month;
		$this->day = $day;
	}
	
	public function setYear($year) {
		$this->year = $year;
	}

	public function getYear() {
		return $this->year;
	}
	
	public function setMonth($month) {
		$this->month = $month;
	}

	public function getMonth() {
		return $this->month;
	}
	
	public function setDay($day) {
		$this->day = $day;
	}

	public function getDay() {
		return $this->day;
	}
	
	public function viewMonth($userId, $year = '', $month = '') {
		if ($year == '') {
			$year = $this->year;
		}
		if ($month == '') {
			$month = $this->month;
		}
		return $this->renderMonthView($userId, $year, $month);
	}
	
	public function viewWeek($userId, $year='', $month = '', $day = '') {
		if ($year == '') {
			$year = $this->year;
		}
		if ($month == '') {
			$month = $this->month;
		}
		if ($day == '') {
			$day = $this->day;
		}
		return $this->renderWeekView($userId, $year, $month, $day);
	}
	
	public function viewDay($userId, $year='', $month = '', $day = '') {
		if ($year == '') {
			$year = $this->year;
		}
		if ($month == '') {
			$month = $this->month;
		}
		if ($day == '') {
			$day = $this->day;
		}
		return $this->renderDayView($userId, $year, $month, $day);
	}
	
	private function renderMonthView($userId, $year, $month) {
		$adjacentDates = $this->getAdjacentMonths($year, $month);
		$html = "<table cellspacing='0' cellpadding='0' class='calGrid'>
					<tr>
						<td class='caption'>
							<a href='javascript:renderMonth({$adjacentDates['yearOfPrevMonth']}, {$adjacentDates['prevMonth']})' title='".L10nHelper::_("Previous month")."'>&lt;&lt;</a>&nbsp;&nbsp;&nbsp;
							<a href='javascript:renderMonth({$adjacentDates['yearOfNextMonth']}, {$adjacentDates['nextMonth']})' title='".L10nHelper::_("Next month")."'>&gt;&gt;</a>
						</td>
						<td class='caption' colspan='5'>".L10nHelper::_("Month")." $month $year</td>
						<td class='caption'>
							<a href='javascript:renderMonth($this->year, $this->month)' title='".L10nHelper::_("Present month")."'>".L10nHelper::_("Present_1")."</a>
						</td>
					</tr>
					<tr class='header'>
						<td style='width: 107px;'>".L10nHelper::_("Mon")."</th>
						<td style='width: 107px;'>".L10nHelper::_("Tue")."</th>
						<td style='width: 107px;'>".L10nHelper::_("Wed")."</th>
						<td style='width: 107px;'>".L10nHelper::_("Thu")."</th>
						<td style='width: 107px;'>".L10nHelper::_("Fri")."</th>
						<td style='width: 107px;'>".L10nHelper::_("Sat")."</td>
						<td style='width: 107px;'>".L10nHelper::_("Sun")."</td>
					</tr>";
		$monthWeekdays = new Calendar_Month_Weekdays($year, $month);
		$monthWeekdays->build();
		$weekDays = 0;
		while ($day = $monthWeekdays->fetch()) {
			$dayInfo = $day->toArray();
			$searchDate = $dayInfo['day'] . "/" . $dayInfo['month'] . "/" . $dayInfo['year'];
			$todayClass = '';
			if ($day->isToday()) {
				$todayClass = 'today';
			}
			if ($weekDays < 5) {
				if ($weekDays == 0) {
					$html .= "<tr>";
				}
				$html .= "<td style='height: 107px' class='$todayClass'>
							  <div style='text-align:right'>
							      <a href='javascript:renderDay({$dayInfo['year']},{$dayInfo['month']},{$dayInfo['day']})'>{$dayInfo['day']}</a>
							  </div>
						      {$this->makeEventsTableForMonth($userId, $searchDate)}
						  </td>";
			} else {
				$html .= "<td style='height: 107px' class='dayOff $todayClass'>
						      <div style='text-align:right'>
								  <a href='javascript:renderDay({$dayInfo['year']},{$dayInfo['month']},{$dayInfo['day']})'>{$dayInfo['day']}</a>
						      </div>
							  {$this->makeEventsTableForMonth($userId, $searchDate)}
						  </td>";
				if ($weekDays == 6) {
					$html .= "</tr>";
				}
			}
		   	$weekDays++;
		   	if ($weekDays > 6) {
				$weekDays = 0;
		   	}
		}
		$html .= "</table>";
		return $html;
	}
	
	private function renderWeekView($userId, $year, $month, $mday) {
		$adjacentDates = $this->getAdjacentWeeks($year, $month, $mday);
		$weekDays = new Calendar_Week($year, $month, $mday);
		$weekDays->build();
		$body = "";
		$i = 0;
		$firstDay = "";
		$lastDay = "";
		while ($day = $weekDays->fetch()) {
			$dayInfo = $day->toArray();
			$searchDate = $dayInfo['day'] . "/" . $dayInfo['month'] . "/" . $dayInfo['year'];
			$weekDay = "Thứ " . ($i + 2);
			$todayClass = '';
			$dayOffClass = '';
			if ($day->isToday()) {
				$todayClass = 'today';
			}
			if (($i % 3) == 0) {
				$body .="<tr>";
				if ($i == 0) {
					$firstDay = $dayInfo;
				} elseif ($i == 6) {
					$lastDay = $dayInfo;
					$weekDay = L10nHelper::_("Sun");
					$dayOffClass = 'dayOff';
				}
			}
			if ($i == 5) {
				$dayOffClass = 'dayOff';
			}
			$body .="<td colspan='2' style='width: 266px; height: 266px;' class='$dayOffClass $todayClass'>
			             <div style='text-align:right'>
						     <a href='javascript:renderDay({$dayInfo['year']},{$dayInfo['month']},{$dayInfo['day']})'>$weekDay ({$dayInfo['day']}/{$dayInfo['month']}/{$dayInfo['year']})</a>
						 </div>
						 {$this->makeEventsTableForWeek($userId, $searchDate)}
					 </td>";
			if (($i % 3) == 2) {
				$body .="</tr>";
			}
			$i++;
		}
		$html = "<table cellspacing='0' cellpadding='0' class='calGrid'>
					<tr>
						<td class='caption'>
							<a href='javascript:renderWeek({$adjacentDates['prevWeek']['year']}, {$adjacentDates['prevWeek']['mon']}, {$adjacentDates['prevWeek']['mday']});' title='".L10nHelper::_("Previous week")."'>&lt;&lt;</a>&nbsp;&nbsp;&nbsp;
							<a href='javascript:renderWeek({$adjacentDates['nextWeek']['year']}, {$adjacentDates['nextWeek']['mon']}, {$adjacentDates['nextWeek']['mday']});' title='".L10nHelper::_("Next week")."'>&gt;&gt;</a>
						</td>
						<td class='caption' colspan='4'>". L10nHelper::_("Week")." {$firstDay['day']}/{$firstDay['month']}/{$firstDay['year']} - {$lastDay['day']}/{$lastDay['month']}/{$lastDay['year']}</td>
						<td class='caption'>
							<a href='javascript:renderWeek({$this->year}, {$this->month}, {$this->day})' title='".L10nHelper::_("Present week")."'>".L10nHelper::_("Present_1")."</a>
						</td>
					</tr>" . $body . "</table>";
		return $html ;
	}
	
	private function renderDayView($userId, $year, $month, $mday) {
		$adjacentDates = $this->getAdjacentDays($year, $month, $mday);
		$day_text = date("D", mktime(0,0,0, $month, $mday, $year) );
		$dayOffClass = '';
		switch($day_text){
			case 'Sun':
				$dayOffClass = 'dayOff';
				$weekDay = L10nHelper::_("Sun");
				break;
			case 'Sat':
				$dayOffClass = 'dayOff';
				$weekDay = L10nHelper::_("Sat");
				break;
			case 'Mon':
				$weekDay = L10nHelper::_('Mon');
				break;
			case 'Tue':
				$weekDay = L10nHelper::_('Tue');
				break;
			case 'Wed':
				$weekDay = L10nHelper::_('Wed');
				break;
			case 'Thu':
				$weekDay = L10nHelper::_('Thu');
				break;
			case 'Fri':
				$weekDay = L10nHelper::_('Fri');
				break;	
		}
		if($day_text == 'Sun' || $day_text == 'Sat'){
			$dayOffClass = 'dayOff';
		}
		$searchDate = $mday. "/" . $month. "/" .$year;
		$body = "";		
		$body .="<td colspan='3' style='height: 266px;' class='$dayOffClass'>
			        <div style='text-align:right'>
						<a href='#'>$weekDay ({$mday}/{$month}/{$year})</a>
					</div>
						 {$this->makeEventsTableForDay($userId, $searchDate)}
					</td>";
		$html = "<table cellspacing='0' cellpadding='0' class='calGrid' width='400px'>
					<tr>
						<td class='caption'>
							<a href='javascript:renderDay({$adjacentDates['prevDay']['year']}, {$adjacentDates['prevDay']['mon']}, {$adjacentDates['prevDay']['mday']});' title='".L10nHelper::_("Previous day")."'>&lt;&lt;</a>&nbsp;&nbsp;&nbsp;
							<a href='javascript:renderDay({$adjacentDates['nextDay']['year']}, {$adjacentDates['nextDay']['mon']}, {$adjacentDates['nextDay']['mday']});' title='".L10nHelper::_("Next day")."'>&gt;&gt;</a>
						</td>
						<td class='caption' >".L10nHelper::_("date ")." {$mday}/{$month}/{$year}</td>
						<td class='caption'>
							<a href='javascript:renderDay({$this->year}, {$this->month}, {$this->day})' title='".L10nHelper::_("Present day")."'>".L10nHelper::_("Present_1")."</a>
						</td>
					</tr>" . $body . "</table>";
		return $html ;
	}
	
	private function getAdjacentMonths($year, $month) {
		$dates['yearOfPrevMonth'] = $year;
		$dates['prevMonth'] = $month - 1;
		$dates['yearOfNextMonth'] = $year;
		$dates['nextMonth'] = $month + 1;
		if ($dates['prevMonth'] == 0) {
			$dates['prevMonth'] = 12;
			$dates['yearOfPrevMonth']--;
		}
		if ($dates['nextMonth'] == 13) {
			$dates['nextMonth'] = 1;
			$dates['yearOfNextMonth']++;
		}
		return $dates;
	}
	
	private function getAdjacentWeeks($year, $month, $mday) {
		$curWeek = mktime(0, 0, 0, $month, $mday, $year);
		$dates['nextWeek'] = getdate($curWeek + 86400*7);
		$dates['prevWeek'] = getdate($curWeek - 86400*7);
		return $dates;
	}
	
	private function getAdjacentDays($year, $month, $mday) {
		$curDay = mktime(0, 0, 0, $month, $mday, $year);
		$dates['nextDay'] = getdate($curDay + 86400);
		$dates['prevDay'] = getdate($curDay - 86400);
		return $dates;
	}
	
	private function makeEventsTableForMonth($userId, $date) {
		$eventsTable = Celini::newORDO('CalendarEvent')->getEvents($userId, $date);
		$html = "<table class='eventTable' cellspacing='0' cellpadding='0'>";
		foreach ($eventsTable as $event) {
			$html .= "<tr>
			              <td>
			                  <a href='javascript:viewDetailOfEvent({$event['calendar_event_id']})'><b>{$this->stripSecond($event['start_time'])}</b> <span style='font-size: 10px;'>{$event['name']}<span></a>
			              </td>
			          </tr>";
		}
		$html .="</table>";
		return $html;
	}
	
	private function makeEventsTableForWeek($userId, $date) {
		$eventsTable = Celini::newORDO('CalendarEvent')->getEvents($userId, $date, 'week');
		$html = "<table class='eventTable' cellspacing='0' cellpadding='0'>";
		foreach ($eventsTable as $event) {
			$html .= "<tr>
			              <td>
			                  <a href='javascript:viewDetailOfEvent({$event['calendar_event_id']})'><b>{$this->stripSecond($event['start_time'])} - {$this->stripSecond($event['end_time'])}</b> <span style='font-size: 10px;'>{$event['name']}<span></a>
			              </td>
			          </tr>";
		}
		$html .="</table>";
		return $html;
	}
	
	private function makeEventsTableForDay($userId, $date) {
		$eventsTable = Celini::newORDO('CalendarEvent')->getEvents($userId, $date, 'day');
		$html = "<table class='eventTable' cellspacing='0' cellpadding='0'>";
		foreach ($eventsTable as $event) {
			$html .= "<tr>
			              <td>
			                  <a href='javascript:viewDetailOfEvent({$event['calendar_event_id']})'><b>{$this->stripSecond($event['start_time'])} - {$this->stripSecond($event['end_time'])}</b> <span style='font-size: 10px;'>{$event['name']}<span></a>
			                  <div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'>{$event['description']}</font></div>
			              </td>
			          </tr>";
		}
		$html .="</table>";
		return $html;
	}
	
	private function stripSecond($time) {
		$timeArray = explode(":", $time);
		return $timeArray[0] . ":" . $timeArray[1];
	}
	
	
}
?>